package cn.edu.nuc.ssm.controller.film;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Comment;
import cn.edu.nuc.ssm.service.interfaces.ContentService;

@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
	private ContentService service;
    
    /**
	 * 通过servlet进入添加评论页面。
	 * @return
	 */
    @RequestMapping(value="add",method=RequestMethod.GET)
    public String getAdd(){
    	return "film/comment/add";
    }
    /**
     * 添加评论（post）
     * @param type
     * @return
     */
    @RequestMapping(value="add",method=RequestMethod.POST)
    public String addType(Comment type){
    	int result = service.insert(type);
    	if(result == 1){
    		return "redirect:list";
    	}
    	return "error";
    } 
	/**
	 * 进入评论列表(查询)
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String getFun(@RequestParam(name="typename",defaultValue="") String typename,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Comment> pager = service.pageList(typename, current);
		model.addAttribute("pager", pager);
		return "film/comment/list";
	}
	
	/**
	 * 更新，（全部进行更新）
	 */
	//获取评论信息
	@RequestMapping(value="detail/{typeid}",method=RequestMethod.GET)
	public String updatetype(@PathVariable("typeid") Integer typeid,Model model){
		Comment type = service.selectByPrimaryKey(typeid);
		model.addAttribute("comment", type);
		return "film/comment/update";
	}
	//更新
	@RequestMapping(value="update/{typeid}",method=RequestMethod.PUT)
	public String updatetype(@PathVariable("typeid") Integer typeid,Comment type){
		int result = service.updateByPrimaryKey(type);
		if(result == 1){
			return "redirect:/comment/list";	
		}
		return  "redirect:error";
	}
	//删除单个评论信息。
	@RequestMapping(value="delete/{typeid}",method=RequestMethod.DELETE)
	public String deleteFun(@PathVariable("typeid") Integer typeid){
		int result = service.deleteByPrimaryKey(typeid);
		if(result==1){
			return "redirect:/comment/list";	
		}
		return  "error";
	}
	
	
	
	

	//发表成功前台显示评论
	@RequestMapping(value="list2",method=RequestMethod.GET)
	public String getFun2(@RequestParam(name="typename",defaultValue="") String typename,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Comment> pager = service.pageList(typename, current);
		model.addAttribute("pager", pager);
		return "forward:/detail.jsp";
	}
	
	//发表成功前台显示评论(点击发表之后得到评论的ajax)
	    @ResponseBody
		@RequestMapping(value="list3",method=RequestMethod.GET)
		public Pager<Comment> getFun3(@RequestParam(name="typename",defaultValue="") String typename,
				@RequestParam(name="current",defaultValue="1") int current , 
				Model model){
			Pager<Comment> pager = service.pageList(typename, current);
			model.addAttribute("pager", pager);
			return pager;
		}
}
